package cn.leon.mysocket;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.dhh.rxlifecycle2.RxLifecycle;
import com.dhh.websocket.RxWebSocket;
import com.dhh.websocket.WebSocketInfo;
import com.dhh.websocket.WebSocketSubscriber;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.WebSocket;
import okio.ByteString;

public class MainActivity extends AppCompatActivity {

    private EditText etContent;
    private TextView tvMsg;
    private ScrollView scrollView;
    private WebSocket mWebSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etContent = (EditText) findViewById(R.id.et_content);
        tvMsg = (TextView) findViewById(R.id.tv_msg);
        scrollView = (ScrollView) findViewById(R.id.scroll_view);
        findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = etContent.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    Toast.makeText(MainActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (null != mWebSocket) {
                    //                    String text = "{\"action\":\"sub\",\"channel\":\"our_depth_4\"}";
                    mWebSocket.send(text);
                }
            }
        });
        testSocket();
    }

    private void testSocket() {
        //正式：      //测试：ws://159.138.2.238:9509/
        RxWebSocket.get("wss://hws.inrobot.com.cn:81/")
                .compose(RxLifecycle.with(this).<WebSocketInfo>bindToLifecycle())
                .subscribe(new WebSocketSubscriber() {
                    @Override
                    public void onOpen(@NonNull WebSocket webSocket) {
                        appendMsgDisplay("onOpen");
                        mWebSocket = webSocket;

                        Map<String, String> params = new HashMap<>();
                        params.put("action", "sub");
                        params.put("channel", "our_depth_4");
                        mWebSocket.send(new Gson().toJson(params));
                    }

                    @Override
                    public void onMessage(@NonNull String text) {
                        appendMsgDisplay("onMessage(String, T):" + text);
                    }

                    @Override
                    protected void onReconnect() {
                        appendMsgDisplay("onReconnect");
                    }

                    @Override
                    protected void onClose() {
                        appendMsgDisplay("onClose");
                    }

                    @Override
                    public void onError(Throwable e) {
                        appendMsgDisplay("onSendDataError:" + e.getMessage());
                    }
                });

    }

    private void appendMsgDisplay(String msg) {
        StringBuilder textBuilder = new StringBuilder();
        if (!TextUtils.isEmpty(tvMsg.getText())) {
            textBuilder.append(tvMsg.getText().toString());
            textBuilder.append("\n");
        }
        textBuilder.append(msg);
        textBuilder.append("\n");
        tvMsg.setText(textBuilder.toString());
        tvMsg.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }
}
